k-means cluster

k-means 군집
install.packages(‘Nclus’)

> cal<-c(52, 160, 89, 57, 34, 32, 30, 69)

> car<-c(112.4, 8.5, 22.8, 14.5, 8.2, 7.7, 7.6, 18.1)

> fat<-c(0.2, 14.7, 1.3, 0.7, 0.2, 0.3, 0.2, 0.2)

> pro<-c(0.3, 2.0, 1.1, 0.3, 0.8, 0.7, 0.6, 0.7)

> fib<-c(2.4, 6.7, 2.6, 2.4, 0.9, 2.0, 0.4, 0.9)

> sug<-c(10.4, 0.7, 12.2, 9.9, 7.9, 4.7, 6.2, 15.5)

> fruits<-data.frame(cal, car, fat, pro, fib, sug)

> rownames(fruits)<-c('apple', 'avocado', 'banana', 'blueberry', 'melon', 'watermelon', 'strawberry', 'grape’)


# 표준화

> fruits<-as.data.frame(scale(fruits, center=T, scale=T))


# k-means의 k=3

> result<-kmeans(fruits, centers=3)

> result$centers

         cal        car        fat        pro         fib        sug

1  2.1853644 -0.4605418  2.4675513  2.1738992  2.24305156 -1.6761980

2 -0.3127448 -0.3305558 -0.3445012 -0.2059483 -0.38337331  0.2085093

3 -0.3088956  2.4438765 -0.4005444 -0.9382091  0.05718828  0.4251423


> fruits$cluster<-result$cluster

> head(fruits)

                  cal        car        fat         pro         fib        sug

apple      -0.3088956  2.4438765 -0.4005444 -0.93820914  0.05718828  0.4251423

avocado     2.1853644 -0.4605418  2.4675513  2.17389922  2.24305156 -1.6761980

banana      0.5456194 -0.0607999 -0.1829647  0.52631244  0.15885634  0.8150817

blueberry  -0.1934206 -0.2928179 -0.3016446 -0.93820914  0.05718828  0.3168258

melon      -0.7246056 -0.4689280 -0.4005444 -0.02288315 -0.70532216 -0.1164402

watermelon -0.7707956 -0.4829050 -0.3807644 -0.20594835 -0.14614784 -0.8096659

           cluster

apple            3

avocado          1

banana           2

blueberry        2

melon            2

watermelon       2